package com.ruoyi.service.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.service.domain.CheServiceItems;
import com.ruoyi.service.domain.CheServiceItemsMid;
import com.ruoyi.service.domain.CheServiceSort;
import com.ruoyi.service.service.ICheServiceItemsMidService;
import com.ruoyi.service.service.ICheServiceItemsService;
import com.ruoyi.service.service.ICheServiceSortService;
import com.ruoyi.shop.api.RemoteShopService;
import com.ruoyi.shop.domain.CheShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.service.domain.CheService;
import com.ruoyi.service.service.ICheServiceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 服务项目Controller
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/service")
public class CheServiceController extends BaseController {
    @Autowired
    private ICheServiceService cheServiceService;
    @Autowired
    private ICheServiceSortService cheServiceSortService;
    @Autowired
    private ICheServiceItemsService cheServiceItemsService;
    @Autowired
    private ICheServiceItemsMidService cheServiceItemsMidService;
    @Autowired
    private RemoteShopService remoteShopService;

    /**
     * 查询服务项目列表
     */
    @RequiresPermissions("service:service:list")
    @GetMapping("/list")
    public TableDataInfo list(CheService cheService) {
        startPage();
        List<CheService> list = cheServiceService.selectCheServiceList(cheService);
        for (CheService che : list) {
            Long shopId = che.getShopId();
            AjaxResult info = remoteShopService.getInfo(shopId, SecurityConstants.INNER);
            CheShop cheShop = JSON.parseObject(JSON.toJSONString(info.get("data")), CheShop.class);
            che.setShop(cheShop);
            System.out.println("remoteShopService:" + info);
            Long serviceSortId = che.getServiceSortId();
            CheServiceSort cheServiceSort1 = cheServiceSortService.selectCheServiceSortById(serviceSortId);
            che.setServiceSort(cheServiceSort1);
            Long id = che.getId();
            List<CheServiceItemsMid> cheServiceItemsMids = cheServiceItemsMidService.selectCheServiceItemsMidByServiceId(id);
            List<CheServiceItems> cheServiceItemsList = new ArrayList<>();
            for (CheServiceItemsMid cheServiceItemsMid : cheServiceItemsMids) {
                Long itemsId = cheServiceItemsMid.getItemId();
                System.out.println("id:" + cheServiceItemsMid.getId());
                System.out.println("itemsId:" + itemsId);
                CheServiceItems cheServiceItems = cheServiceItemsService.selectCheServiceItemsById(itemsId);
                cheServiceItemsList.add(cheServiceItems);
            }
            che.setServiceItems(cheServiceItemsList);
        }
        return getDataTable(list);
    }

    /*
        根据商户id查询服务列表
     */
    @RequiresPermissions("service:service:list")
    @GetMapping(value = "/byshopid/{shop_id}")
    public TableDataInfo getListByShopId(@PathVariable("shop_id") Long shop_id) {
        startPage();
        List<CheService> list = cheServiceService.selectCheServiceListByShopId(shop_id);
        return getDataTable(list);
    }

    /**
     * 导出服务项目列表
     */
    @RequiresPermissions("service:service:export")
    @Log(title = "服务项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheService cheService) {
        List<CheService> list = cheServiceService.selectCheServiceList(cheService);
        ExcelUtil<CheService> util = new ExcelUtil<CheService>(CheService.class);
        util.exportExcel(response, list, "服务项目数据");
    }

    /**
     * 获取服务项目详细信息
     */
    @RequiresPermissions("service:service:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CheService che = cheServiceService.selectCheServiceById(id);
        Long shopId = che.getShopId();
        AjaxResult info = remoteShopService.getInfo(shopId, SecurityConstants.INNER);
        CheShop cheShop = JSON.parseObject(JSON.toJSONString(info.get("data")), CheShop.class);
        che.setShop(cheShop);
        System.out.println("remoteShopService:" + info);
        Long serviceSortId = che.getServiceSortId();
        CheServiceSort cheServiceSort1 = cheServiceSortService.selectCheServiceSortById(serviceSortId);
        che.setServiceSort(cheServiceSort1);
        Long cheId = che.getId();
        List<CheServiceItemsMid> cheServiceItemsMids = cheServiceItemsMidService.selectCheServiceItemsMidByServiceId(cheId);
        List<CheServiceItems> cheServiceItemsList = new ArrayList<>();
        for (CheServiceItemsMid cheServiceItemsMid : cheServiceItemsMids) {
            Long itemsId = cheServiceItemsMid.getItemId();
            System.out.println("id:" + cheServiceItemsMid.getId());
            System.out.println("itemsId:" + itemsId);
            CheServiceItems cheServiceItems = cheServiceItemsService.selectCheServiceItemsById(itemsId);
            cheServiceItemsList.add(cheServiceItems);
        }
        che.setServiceItems(cheServiceItemsList);
        return success(che);
    }

    /**
     * 新增服务项目
     */
    @RequiresPermissions("service:service:add")
    @Log(title = "服务项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheService cheService) {
        return toAjax(cheServiceService.insertCheService(cheService));
    }

    /**
     * 修改服务项目
     */
    @RequiresPermissions("service:service:edit")
    @Log(title = "服务项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheService cheService) {
        return toAjax(cheServiceService.updateCheService(cheService));
    }

    /**
     * 删除服务项目
     */
    @RequiresPermissions("service:service:remove")
    @Log(title = "服务项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cheServiceService.deleteCheServiceByIds(ids));
    }
}
